package com.xxx.system.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.blog.domain.ArticleType;
import com.xxx.system.domain.Menu;
import com.xxx.system.query.MenuQuery;
import com.xxx.system.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
@Api(tags = "菜单接口类")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    @GetMapping("/getTypeTree")
    @ApiOperation(value = "查询类型树")
    public R<List<Menu>> getTypeTree() {
        List<Menu> menuList = menuService.getTypeTree();
        return R.ok(menuList);
    }

    @GetMapping
    @ApiOperation(value = "查询所有")
    public R<List<Menu>> findAll() {
        List<Menu> list = menuService.findAll();
        return R.ok(list);
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public R<Menu> findOne(@PathVariable Long id) {
        Menu menu = menuService.findOne(id);
        return R.ok(menu);
    }
    @PostMapping
    @ApiOperation(value = "分页条件查询")
    public R<PageInfo<Menu>> queryPage(@RequestBody MenuQuery menuQuery) {
        List<Menu> list = menuService.queryPage(menuQuery);
        return R.ok(new PageInfo<Menu>(list));
    }

    @PutMapping
    @ApiOperation(value = "添加或者删除")
    public R<Void> queryPage(@RequestBody Menu menu) {
        try {
            if(menu.getId()==null){
                menuService.addMenu(menu);
            }else {
                menuService.updateMenu(menu);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("用户已存在");
        }
        return R.ok();
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public R<Void> deleteMenu(@PathVariable Long id) {
        try {
            menuService.deleteMenu(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }
    @PatchMapping
    @ApiOperation(value = "根据多个id删除")
    public R<Void> batchMenu(@RequestBody Long[] ids) {
        try {
            menuService.batchMenu(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @GetMapping("/menuTree/{id}")
    @ApiOperation(value = "查询类型树")
    public R<List<Menu>> getTypeTree2(@PathVariable Long id) {
        List<Menu> menuTypes = menuService.getTypeTree2(id);
        return R.ok(menuTypes);
    }
}
